package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.zhentao.common.constants.HotArticleConstants;
import com.zhentao.mapper.ArticleMapper;
import com.zhentao.model.article.dto.CountDto;
import com.zhentao.model.article.dto.LikesBehaviorDto;
import com.zhentao.model.article.pojo.ApArticle;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.mess.UpdateArticleMess;
import com.zhentao.model.user.pojo.ApUser;
import com.zhentao.service.BehaviorService;
import com.zhentao.utils.common.AppUserThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

@Service
public class BehaviorServiceImpl implements BehaviorService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @Override
    public ResponseResult likesBehavior(LikesBehaviorDto dto) {
        ApUser user = AppUserThreadLocalUtil.getUser();
        if(user== null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        UpdateArticleMess mess =new UpdateArticleMess();
        mess.setType(UpdateArticleMess.UpdateArticleType.LIKES);
        ApArticle apArticle = articleMapper.selectById(dto.getArticleId());
        mess.setArticleId(dto.getArticleId());
        if(dto.getOperation() == 0){
            apArticle.setLikes(apArticle.getLikes()+1);
            redisTemplate.opsForHash().put("likes"+dto.getArticleId().toString(),user.getId().toString() ,JSON.toJSONString(dto) );
            mess.setAdd(1);
        }else if(dto.getOperation() == 1){
            apArticle.setLikes(apArticle.getLikes()-1);
            redisTemplate.opsForHash().delete("likes"+dto.getArticleId().toString(),user.getId().toString());
            mess.setAdd(-1);
        }
        articleMapper.updateById(apArticle);
        //发送消息，数据聚合
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));
        return ResponseResult.okResult(dto);
    }

    @Override
    public ResponseResult read_behavior(CountDto dto) {
        ApUser user = AppUserThreadLocalUtil.getUser();
        if(user== null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        ApArticle apArticle = articleMapper.selectById(dto.getArticleId());
        apArticle.setViews(apArticle.getViews()+dto.getCount());
        redisTemplate.opsForZSet().add("read_behavior"+dto.getArticleId().toString() ,user.getId().toString(),System.currentTimeMillis() );
        articleMapper.updateById(apArticle);
        //发送消息，数据聚合
        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setType(UpdateArticleMess.UpdateArticleType.VIEWS);
        mess.setAdd(1);
        kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,JSON.toJSONString(mess));
        return ResponseResult.okResult(dto);
    }


    @Override
    public ResponseResult unLikesBehavior(CountDto dto) {
        ApUser user = AppUserThreadLocalUtil.getUser();
        if(user== null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        if(dto.getType()==0){
            redisTemplate.opsForHash().put("unLikesBehavior"+dto.getArticleId().toString(),user.getId().toString(), JSON.toJSONString(dto));
        }else {
            redisTemplate.opsForHash().delete("unLikesBehavior"+dto.getArticleId().toString(),user.getId().toString());
        }

        return ResponseResult.okResult(dto);
    }
}
